<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-14 14:54:32
// +-----------------------------------------------------------------------



/**
 * 数据报表控制器
 * @authors 琥珀 (317559272@qq.com)
 */
namespace Admin\Controller;
use Think\Controller;
Class ReportController extends Controller
{

    //按照客服获取当天昨天当月上个月数据
    public function index() {
        $hospital = "深圳的尚研究院";
        $map['status'] = 1;
        $users = M("Admin")->where($map)->select();

        //今日数据
        $count = M('Order');
        $count_today = $count->where()->count();
        $count_today_come = $count->where()->count();
        $count_today_not = $count_today - $count_today_come;

        //昨天数据
        $count_yesterday = $count->where()->count();
        $count_yesterday_come = $count->where()->count();
        $count_yesterday_not = $count_yesterday - $count_yesterday_come;

        //当月数据
        $count_thismonth = $count->where()->count();
        $count_thismonth_come = $count->where()->count();
        $count_thismonth_not = $count_thismonth - $count_thismonth_come;

        //上月数据
        $count_lastmonth = $count->where()->count();
        $count_lastmonth_come = $count->where()->count();
        $count_lastmonth_not = $count_lastmonth - $count_lastmonth_come;

        //所有
        $count_all = $count->where()->count();
        $count_all_come = $count->where()->count();
        $count_all_not = $count_all - $count_all_come;
        $this->display();
    }
    public function barchart() {
        $this->assign('data', $data);
        $this->display();
    }

    public function report() {
        $option = array();
        for ($i = 0; $i <= 12; $i++) {
            $time = mktime(0, 0, 0, date("m") - $i, 1);
            $option[] = '<a href="/report/report/time/' . $time . '" data-toggle="navtab" data-id="report2">' . date("Y-m", $time) . '</a>';
        }
        $this->assign('option', $option);
        $time = I('time') ? I('time') : mktime(0, 0, 0, date("m"), 1);
        $this->assign('time', $time);
        $this->display();
    }
    public function data() {
        $model = M('Order');
        $timebegin = I('time') ? I('time') : mktime(0, 0, 0, date("m"), 1);
        $timeend = strtotime("+1 month", $timebegin);

        //获取x轴时间以日期为单位
        $year = date('Y', $timebegin);
        $month = date('m', $timebegin);
        $j = days_in_month($month, $year);
         //获取月份天数
        $start_time = strtotime(date("." . $year . "-" . $month . "-01" . "'"));
         //获指定月份第一天时间戳
        $x = array();
        for ($i = 0; $i < $j; $i++) {
            $x[] = date('d', $start_time + $i * 86400);
             //每隔一天赋值给数组

        }

        //获取类型
        $map['addtime'] = array(array('gt', $timebegin), array('lt', $timeend));
        $rs = $model->where($map)->field('id,addtime,status')->select();
        $where['status'] = 1;
        $rs1 = $model->where($map)->where($where)->field('id,addtime,status')->select();
        $count = array();
        foreach ($rs as $value) {
            $count[date("j", $value["addtime"]) ] = $count[date("j", $value["addtime"]) ] + 1;
        }
        $count_come = array();
        foreach ($rs1 as $value) {
            $count_come[date("j", $value["addtime"]) ] = $count_come[date("j", $value["addtime"]) ] + 1;
        }
        $count = array_values($count);
        $count_come = array_values($count_come);

        //组建数据数据
        $data = ["tooltip" => ["trigger" => "axis"], "legend" => ["data" => ["预约", "已到"]], "toolbox" => ["show" => true, "feature" => ["mark" => ["show" => true], "dataView" => ["show" => true, "readOnly" => false], "magicType" => ["show" => true, "type" => ["line", "bar"]], "restore" => ["show" => true], "saveAsImage" => ["show" => true]]], "calculable" => true, "xAxis" => [["type" => "category", "data" => $x]], "yAxis" => [["type" => "value", "splitArea" => ["show" => true]]], "series" => [["name" => "预约","type" => "bar", "data" => $count], ["name" => "已到", "type" => "bar", "data" => $count_come]]];
        $datas = json_encode($data, true);
        echo $datas;
    }
}
?>